/**
 * Created by Administrator on 2017/3/1 0001.
 */
var Scroller={
    create:function (target,pageWidth,pageHeight,pageDir,_pageMax) {
        var sc={pageIndex:0,pageMax:_pageMax};
        var bool_down;

        var mouseDownPT={x:0,y:0};
        var targetStartPT={x:0,y:0};
        var threhold=100;
        var tw_page;

        target.interactive=true;
        target.on('pointerdown',downHandler);
        target.on('pointerup', upHandler)
        target.on('pointermove',moveHandler);

        sc.scrollToPage=function (pageIdx) {

        }
        sc.scrollToPosition=function (x,y) {

        }

        function downHandler(e) {
            bool_down=true;
            if (tw_page) {
                tw_page.stop();
                TWEEN.remove(tw_page);
                tw_page=null;
            }
            mouseDownPT.x = e.data.global.x;
            mouseDownPT.y = e.data.global.y;
            targetStartPT.x = target.x;
            targetStartPT.y = target.y;
        }
        function upHandler(e) {
            bool_down = false;
            if(pageDir=='ver') {
                if (target.y < -sc.pageIndex * pageHeight - threhold) {

                    target.interactive = false;
                    if (sc.pageIndex != sc.pageMax) {
                        var event = new CustomEvent("Game_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);
                        if (tw_page) {
                            tw_page.stop();
                            TWEEN.remove(tw_page);
                            tw_page = null;
                        }
                        tw_page = new TWEEN.Tween({y: target.y})
                            .to({y: -pageHeight * (sc.pageIndex + 1)}, 700)
                            .easing(TWEEN.Easing.Back.Out)
                            .onUpdate(function () {
                                target.y = this.y;
                            })
                            .onComplete(function () {
                                sc.pageIndex++;
                                target.interactive = true;
                                var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                                document.dispatchEvent(event);
                            })
                            .start();
                    }
                    else {
                        back();
                    }

                }
                else if (target.y > -sc.pageIndex * pageHeight + threhold) {
                    if (sc.pageIndex != 0) {
                        target.interactive = false;
                        var event = new CustomEvent("Game_ScrollStart", {detail:sc.pageIndex });
                        document.dispatchEvent(event);
                        if (tw_page) {
                            tw_page.stop();
                            TWEEN.remove(tw_page);
                            tw_page = null;
                        }
                        tw_page = new TWEEN.Tween({y: target.y})
                            .to({y: -pageHeight * (sc.pageIndex - 1)}, 700)
                            .easing(TWEEN.Easing.Back.Out)
                            .onUpdate(function () {
                                target.y = this.y;
                            })
                            .onComplete(function () {
                                sc.pageIndex--;
                                target.interactive = true;
                                var event = new CustomEvent("Game_ScrollComplete", {detail:sc.pageIndex });
                                document.dispatchEvent(event);
                            })
                            .start();
                    }
                    else {
                        back();
                    }
                }

                else {
                    back();
                }
            }
            else if(pageDir=='hor')
            {

                if (target.x < -sc.pageIndex * pageWidth - threhold) {
                    target.interactive = false;

                    if (sc.pageIndex != sc.pageMax) {
                        if (tw_page) {
                            tw_page.stop();
                            TWEEN.remove(tw_page);
                            tw_page = null;
                        }

                        tw_page = new TWEEN.Tween({x: target.x})
                            .to({x: -pageWidth * (sc.pageIndex + 1)}, 700)
                            .easing(TWEEN.Easing.Back.Out)
                            .onUpdate(function () {
                                target.x = this.x;
                            })
                            .onComplete(function () {
                                sc.pageIndex++;
                                target.interactive = true;
                            })
                            .start();
                    }
                    else {
                        back();
                    }

                }
                else if (target.x > -sc.pageIndex * pageWidth + threhold) {
                    if (sc.pageIndex != 0) {
                        target.interactive = false;
                        if (tw_page) {
                            tw_page.stop();
                            TWEEN.remove(tw_page);
                            tw_page = null;
                        }
                        tw_page = new TWEEN.Tween({x: target.x})
                            .to({x: -pageWidth * (sc.pageIndex - 1)}, 700)
                            .easing(TWEEN.Easing.Back.Out)
                            .onUpdate(function () {
                                target.x = this.x;
                            })
                            .onComplete(function () {
                                sc.pageIndex--;
                                target.interactive = true;
                            })
                            .start();
                    }
                    else {
                        back();
                    }
                }

                else {
                    back();
                }
            }
        }
        function moveHandler(e) {

            if (bool_down) {
                if(pageDir=='ver')
                {
                    var mouseY=e.data.global.y;
                    if (sc.pageIndex == 0 && mouseY > mouseDownPT.y) {
                        target.y=targetStartPT.y+(mouseY - mouseDownPT.y)/4;
                    }
                    else if (sc.pageIndex == sc.pageMax && mouseY < mouseDownPT.y) {
                        target.y=targetStartPT.y+(mouseY-mouseDownPT.y)/4;
                    }
                    else {
                        target.y=targetStartPT.y+(mouseY-mouseDownPT.y);
                    }
                }
                else if(pageDir=='hor')
                {
                    var mouseX=e.data.global.x;
                    if (sc.pageIndex == 0 && mouseX > mouseDownPT.x) {
                        target.x=targetStartPT.x+(mouseX - mouseDownPT.x)/4;
                    }
                    else if (sc.pageIndex == sc.pageMax && mouseX < mouseDownPT.x) {
                        target.x=targetStartPT.x+(mouseX-mouseDownPT.x)/4;
                    }
                    else {
                        target.x=targetStartPT.x+(mouseX-mouseDownPT.x);
                    }
                }
            }
        }
        function back()
        {
            if (tw_page) {
                tw_page.stop();
                TWEEN.remove(tw_page);
                tw_page=null;
            }


            if(pageDir=='ver')
            {
                var time = 1000 * Math.abs(target.y - (sc.pageIndex * -pageHeight)) / 400;
                tw_page=new TWEEN.Tween({y:target.y})
                    .to({y:sc.pageIndex * -pageHeight },time)
                    .easing(TWEEN.Easing.Back.Out)
                    .onUpdate(function(){
                        target.y=this.y;
                    })
                    .onComplete(function () {
                        target.interactive=true;
                    })
                    .start();
            }
            else if(pageDir=='hor')
            {
                var time = 1000 * Math.abs(target.x - (sc.pageIndex * -pageWidth)) / 400;
                tw_page=new TWEEN.Tween({x:target.x})
                    .to({x:sc.pageIndex * -pageWidth },time)
                    .easing(TWEEN.Easing.Back.Out)
                    .onUpdate(function(){
                        target.x=this.x;
                    })
                    .onComplete(function () {
                        target.interactive=true;
                    })
                    .start();
            }
        }
        return sc;
    }

}